package com.macro.mall.portal.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.mapper.PmsProductMapper;
import com.macro.mall.mapper.UmsMemberReadHistoryMapper;
import com.macro.mall.model.PmsProduct;
import com.macro.mall.model.UmsMember;
import com.macro.mall.model.UmsMemberReadHistory;
import com.macro.mall.model.UmsMemberReadHistoryExample;
import com.macro.mall.portal.service.MemberReadHistoryService;
import com.macro.mall.portal.service.UmsMemberService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 会员浏览记录管理Service实现类
 *
 * @author macro
 * @date 2018/8/3
 */
@Service
@AllArgsConstructor
public class MemberReadHistoryServiceImpl implements MemberReadHistoryService {

    private final PmsProductMapper productMapper;
    private final UmsMemberService memberService;
    private final UmsMemberReadHistoryMapper umsMemberReadHistoryMapper;

    @Override
    public int create(UmsMemberReadHistory memberReadHistory) {
        if (memberReadHistory.getProductId() == null) {
            return 0;
        }
        UmsMember member = memberService.getCurrentMember();
        memberReadHistory.setMemberId(member.getId());
        memberReadHistory.setMemberNickname(member.getNickname());
        memberReadHistory.setMemberIcon(member.getIcon());
        PmsProduct product = productMapper.selectByPrimaryKey(memberReadHistory.getProductId());
        if (product == null || product.getDeleteStatus() == 1) {
            return 0;
        }
        memberReadHistory.setProductName(product.getName());
        memberReadHistory.setProductSubTitle(product.getSubTitle());
        memberReadHistory.setProductPrice(product.getPrice() + "");
        memberReadHistory.setProductPic(product.getPic());
        //先查询该记录是否存在于历史表里，如果不存在，再新增，如果存在，就更新其创建时间
        UmsMemberReadHistoryExample example = new UmsMemberReadHistoryExample();
        example.createCriteria().andMemberIdEqualTo(member.getId())
               .andProductIdEqualTo(memberReadHistory.getProductId());
        List<UmsMemberReadHistory> umsMemberReadHistories = umsMemberReadHistoryMapper.selectByExample(example);
        if (umsMemberReadHistories.isEmpty()) {
            return umsMemberReadHistoryMapper.insertSelective(memberReadHistory);
        }
        UmsMemberReadHistory umsMemberReadHistory = umsMemberReadHistories.get(0);
        umsMemberReadHistory.setCreateTime(new Date());
        return umsMemberReadHistoryMapper.updateByPrimaryKey(umsMemberReadHistory);
    }

    @Override
    public int delete(List<Long> ids) {
        for (Long id : ids) {
            umsMemberReadHistoryMapper.deleteByPrimaryKey(id);
        }
        return ids.size();
    }

    @Override
    public List<UmsMemberReadHistory> list(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        UmsMember member = memberService.getCurrentMember();
        UmsMemberReadHistoryExample example = new UmsMemberReadHistoryExample();
        example.createCriteria()
               .andMemberIdEqualTo(member.getId());
        example.setOrderByClause("create_time desc");
        return umsMemberReadHistoryMapper.selectByExample(example);
    }

    @Override
    public void clear() {
        UmsMember member = memberService.getCurrentMember();
        UmsMemberReadHistoryExample example = new UmsMemberReadHistoryExample();
        example.createCriteria()
               .andMemberIdEqualTo(member.getId());
        umsMemberReadHistoryMapper.deleteByExample(example);
    }
}
